package com.atguigu.dga.assessor.spec;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.meta.constant.MetaConst;
import net.minidev.json.JSONUtil;
import org.apache.hadoop.hbase.client.coprocessor.BigDecimalColumnInterpreter;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.List;

@Component(value = "FIELD_COMMENT")
public class FieldCommentAssessor extends Assessor {
    @Override
    public void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) {

        String colNameJson = assessParam.getTableMetaInfo().getColNameJson();
        //把json字符串转为json对象数组
        List<JSONObject> jsonObjects = JSON.parseArray(colNameJson, JSONObject.class);

        //创建储存无备注信息的字段的集合
        HashSet<String> strings = new HashSet<>();

        for (JSONObject jsonObject : jsonObjects) {
            String comment = jsonObject.getString("comment");
            if (comment==null || comment.trim().isEmpty()){
                strings.add(jsonObject.getString("name"));
            }
        }
        if (strings.size()>0){
            if (strings.size()==jsonObjects.size() || jsonObjects.size()==0){
                //分数置零
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            }else {
                //根据比例给分
                double i = strings.size() *1.0/ jsonObjects.size();
                double m=governanceAssessDetail.getAssessScore().intValue()*(1-i);
                governanceAssessDetail.setAssessScore(BigDecimal.valueOf(m));
            }
            StringBuilder stringBuilder = new StringBuilder("以下字段无字段备注: ");
            for (String string : strings) {
                stringBuilder.append(string+",");
            }

            governanceAssessDetail.setAssessProblem(stringBuilder.toString());
            //获取通用路径
            String governanceUrl = assessParam.getGovernanceMetric().getGovernanceUrl();
            //路径替换
            governanceUrl=governanceUrl.replace("{tableId}",assessParam.getTableMetaInfo().getId() + "");
            governanceAssessDetail.setGovernanceUrl(governanceUrl);
        }

    }
}
